package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_Teleport2;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DungeonRTable {
  private static final Log _log = LogFactory.getLog(DungeonRTable.class);
  
  private static DungeonRTable _instance = null;
  
  private static Map<String, ArrayList<int[]>> _dungeonMap = new HashMap<>();
  
  private static Map<String, Integer> _dungeonMapID = new HashMap<>();
  
  private static Random _random = new Random();
  
  public static DungeonRTable get() {
    if (_instance == null)
      _instance = new DungeonRTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int teltportId = 1000;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `dungeon_random`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int srcMapId = rs.getInt("src_mapid");
        int srcX = rs.getInt("src_x");
        int srcY = rs.getInt("src_y");
        String key = "" + srcMapId + srcX + srcY;
        if (_dungeonMap.containsKey(key)) {
          _log.error("相同SRC(多点)传送座标(" + key + ")");
          continue;
        } 
        int heading = rs.getInt("new_heading");
        ArrayList<int[]> value = (ArrayList)new ArrayList<>();
        if (rs.getInt("new_x1") != 0) {
          int[] newLoc = { rs.getInt("new_x1"), rs.getInt("new_y1"), rs.getShort("new_mapid1"), 
              heading };
          value.add(newLoc);
        } 
        if (rs.getInt("new_x2") != 0) {
          int[] newLoc = { rs.getInt("new_x2"), rs.getInt("new_y2"), rs.getShort("new_mapid2"), 
              heading };
          value.add(newLoc);
        } 
        if (rs.getInt("new_x3") != 0) {
          int[] newLoc = { rs.getInt("new_x3"), rs.getInt("new_y3"), rs.getShort("new_mapid3"), 
              heading };
          value.add(newLoc);
        } 
        if (rs.getInt("new_x4") != 0) {
          int[] newLoc = { rs.getInt("new_x4"), rs.getInt("new_y4"), rs.getShort("new_mapid4"), 
              heading };
          value.add(newLoc);
        } 
        if (rs.getInt("new_x5") != 0) {
          int[] newLoc = { rs.getInt("new_x5"), rs.getInt("new_y5"), rs.getShort("new_mapid5"), 
              heading };
          value.add(newLoc);
        } 
        _dungeonMap.put(key, value);
        _dungeonMapID.put(key, Integer.valueOf(teltportId++));
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("载入地图切换点设置(多点)数量: " + _dungeonMapID.size() + "(" + timer.get() + "ms)");
  }
  
  public boolean dg(int locX, int locY, int mapId, L1PcInstance pc) {
    String key ="" + mapId + locX + locY;
    if (_dungeonMap.containsKey(key)) {
      ArrayList<int[]> newLocs = _dungeonMap.get(key);
      int rnd = _random.nextInt(newLocs.size());
      int[] loc = newLocs.get(rnd);
      int newX = loc[0];
      int newY = loc[1];
      short newMap = (short)loc[2];
      int heading = loc[3];
      pc.setSkillEffect(78, 2000);
      pc.stopHpRegeneration();
      pc.stopMpRegeneration();
      int teleportId = ((Integer)_dungeonMapID.get(key)).intValue();
      teleport(pc, teleportId, newX, newY, newMap, heading, false);
      return true;
    } 
    return false;
  }
  
  private void teleport(L1PcInstance pc, int id, int newX, int newY, short newMap, int heading, boolean b) {
    pc.setTeleportX(newX);
    pc.setTeleportY(newY);
    pc.setTeleportMapId(newMap);
    pc.setTeleportHeading(heading);
    pc.sendPackets((ServerBasePacket)new S_Teleport2(newMap, id));
  }
}
